package TUILayer;
import ControlLayer.*;
import ModelLayer.*;
import java.util.Scanner;

/**
 * This is the secound menu, Addressbook menu.
 * 
 * @author (Group2) 
 * @version (2)
 */

 
public class AddressBookUI
{
    // instance variables 
     private AddressCtr addressControl;

    /**
     * Constructor for objects of class AddressBookUI
     */
    public AddressBookUI()
    {
          addressControl=new AddressCtr();
    }

     
    // // Here are the 6 options that you have  
    public void AddressBookmenu()
    {
       boolean exit=false;
       while (!exit) //! means while exit not is true (that is: false)
       {
           int choise = AddressbookText();
           switch (choise)
            {
                case 1:
                {
                    // search for a person and return the information about it.
                     String i="";
                     while (!i.equals("1"))
                      {
                          Scanner keyboard = new Scanner(System.in);  
                          System.out.println("Please type the user id that you want more info:");
                          String id=keyboard.nextLine();
                          addressControl.getPerson(id);              
                          System.out.println("Press 1 to return");
                          System.out.println("Press enter to search for another person.");
                          String var=keyboard.nextLine();
                          i=var;
                        }   
                          break;
                }
                case 2:
                {
                    // add a new person to the sistem
                     String i="";
                     while (!i.equals("1"))
                      {
                        Scanner keyboard = new Scanner(System.in);  
                                                
                        addPerson();
                        System.out.println("Press 1 to return");
                        System.out.println("Press enter to add another person.");
                        String var=keyboard.nextLine();
                        i=var;
                      }
                       break;
                }
                case 3:
                {
                     // delete a person from the sistem
                     String i="";
                      while (!i.equals("1"))
                      {
                        Scanner keyboard = new Scanner(System.in);  
                        System.out.println("Please type the user id that you want to delete:");
                        String id=keyboard.nextLine();
                        
                        addressControl.deletePerson(id);
                        System.out.println("Press 1 to return");
                        System.out.println("Press enter to delete another person.");
                        String var=keyboard.nextLine();
                        i=var;
                      }
                      AddressBookmenu();
                }
                case 4:
                {
                    // update a person fields
                    UpdateMainMenu();
                    
                    break;
                }
                case 5:
                {
                    //list all persons
                   
                    String i="";
                    while (!i.equals("1"))
                    {
                       Scanner keyboard = new Scanner(System.in);
                       addressControl.getListOfPersons();
                       System.out.println("Press 1 to return");
                       System.out.println("Press enter to print the list again.");
                       String var=keyboard.nextLine();
                       i=var;
                      }
                       break;
                }
                case 6:
                {
                    //return to main menu
                    exit = true;        
                    break;
                }
            }
            }
        }
        
              
    private void UpdateMainMenu()
    {
       boolean exit=false;
       while (!exit) //! means while exit not is true (that is: false)
       {
           int choise = textUpdateMenu();
           switch (choise)
            {
                case 1:
                {
                     // update a person's name
                     Scanner keyboard = new Scanner(System.in);  
                     System.out.println("Please type user Id that you want to change name");
                     String id=keyboard.nextLine();
                     
                     System.out.println("Please type the new name");
                     String name=keyboard.nextLine();
                     
                     addressControl.updateName(id,name);
                       break;
                }
                case 2:
                {
                    // update a person's address 
                    Scanner keyboard = new Scanner(System.in);  
                     System.out.println("Please type user Id that you want to change address");
                     String id=keyboard.nextLine();
                     
                     System.out.println("Please type the new address");
                     String address=keyboard.nextLine();
                     
                     addressControl.updateAddress(id,address);
                       break;
                }
                case 3:
                { 
                    // update a person's postal code
                    Scanner keyboard = new Scanner(System.in);  
                     System.out.println("Please type user Id that you want to change postal code");
                     String id=keyboard.nextLine();
                     
                     System.out.println("Please type the new postal code");
                     int postalCode=keyboard.nextInt();
                     
                     addressControl.updatePostalCode(id,postalCode);
                       break;
                    
                }
                case 4:
                {
                    // update a person's city 
                    Scanner keyboard = new Scanner(System.in);  
                     System.out.println("Please type user Id that you want to change city");
                     String id=keyboard.nextLine();
                     
                     System.out.println("Please type the new city");
                     String city=keyboard.nextLine();
                     
                     addressControl.updateCity(id,city);
                       break;
                }
                case 5:
                {
                   // update a person's phone number
                    Scanner keyboard = new Scanner(System.in);  
                     System.out.println("Please type user Id that you want to change phone");
                     String id=keyboard.nextLine();
                     
                     System.out.println("Please type the new phone:");
                     String phone=keyboard.nextLine();
                     
                     addressControl.updatePhone(id,phone);
                     break;
                }
                case 6:
                {
                    //return to addressbook menu
                    
                    exit = true;        
                    break;
                }
            }
            }
        }
        
    // display the option that you have     
    private int AddressbookText()
    {
        // creates a keyboard object to read input
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** AddressBook ***");
        System.out.println(" (1)Show Information About Person");
        System.out.println(" (2)Add person");
        System.out.println(" (3)Delete Person");
        System.out.println(" (4)Update Person");
        System.out.println(" (5)List all persons");
        System.out.println(" (6)Return");
        System.out.print("\n Make your choise: ");

        int choise2 = keyboard.nextInt();

        return choise2;
    }
    
    // display what info you should type in order to create a person 
    private void addPerson()
    {
        // creates a keyboard object to read input
        Scanner keyboard = new Scanner(System.in);
        System.out.println("  Write Id:");
        String id=keyboard.nextLine();
        
        System.out.println(" Write name:");
        String name=keyboard.nextLine();
        
        System.out.println(" Write address:");
        String address=keyboard.nextLine();
        
        System.out.println(" Write postal code:");
        int postalCode=keyboard.nextInt();
        keyboard.nextLine();
        
        System.out.println(" Write city");
        String city=keyboard.nextLine();
                
        System.out.println(" Write phone");
        String phone=keyboard.nextLine();
        
        addressControl.createPerson(id,name,address,postalCode,city,phone);
        
    }
    
    // ask for a person id and then it delete it
    public void deletePerson()
    {
        // creates a keyboard object to read input
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please insert the id of the person:");
        String id = keyboard.nextLine();    

        addressControl.deletePerson(id);
    }
    
    // display what info you should type in order to update a person's info
    private int textUpdateMenu()
    {
        // creates a keyboard object to read input
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** UpdateMenu ***");
        System.out.println(" Chose what do you want to modify");
        System.out.println(" (1)Name");
        System.out.println(" (2)Address");
        System.out.println(" (3)Postal Code");
        System.out.println(" (4)City");
        System.out.println(" (5)Phone");
        System.out.println(" (6)Return");
        System.out.print("\n Make your choise: ");

        int choise2 = keyboard.nextInt();

        return choise2;
    }
    
   
}
